package com.fengxing.ams.web.facade;

import java.text.MessageFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.fengxing.ams.pub.AMSException;
import com.fengxing.ams.pub.manager.intf.IConfigManager;
import com.fengxing.ams.web.action.view.VerifyCodeVO;
import com.fengxing.ams.web.facade.intf.INotifyFacade;
import com.fengxing.ams.web.facade.mouble.notify.SendEmailVO;

@Component
@Transactional
public class NotifyFacade implements INotifyFacade {
	
	@Autowired
	IConfigManager configManager;

	@Override
	public void pushEmail(SendEmailVO vo) {
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your accessKey>", "<your accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
		
	}
	
	private static SingleSendMailRequest convertToRequest(SendEmailVO vo) {
		if (vo == null)
			return null;
		SingleSendMailRequest request = new SingleSendMailRequest();
		 request.setAccountName(vo.getFromAddress());
		 request.setAcceptFormat(FormatType.JSON);
         request.setAddressType(0);
         request.setTagName("verifyCode");
         request.setReplyToAddress(false);
         request.setToAddress(vo.getToAddress());
         request.setSubject(vo.getSubject());
         request.setHtmlBody(vo.getHtmlBody());
		return request;
	}

	@Override
	public void sendVerifyEmail(VerifyCodeVO vo) {
		String fromAddress = this.configManager.getProperty("no_reply_email", "no-reply@m.gaokaozyt.cn");
		String htmlTemplate = this.configManager.getProperty("verify_template", "");
		String accessKey = this.configManager.getProperty("access_key", "");
		String access_key_secret = this.configManager.getProperty("access_key_secret", "");
		IClientProfile profile = DefaultProfile.getProfile("cn-beijing", accessKey, access_key_secret);
        IAcsClient client = new DefaultAcsClient(profile);
        
        SingleSendMailRequest req = convertToRequest(vo, fromAddress, htmlTemplate);
        try {
			SingleSendMailResponse httpResponse = client.getAcsResponse(req);
		} catch (ServerException e) {
			throw new AMSException("发送邮件错误", e);
		} catch (ClientException e) {
			throw new AMSException("发送邮件错误", e);
		}
	}
	
	private static SingleSendMailRequest convertToRequest(VerifyCodeVO vo, String fromAddress, String htmlTemplate) {
		if (vo == null)
			return null;
		
		SingleSendMailRequest request = new SingleSendMailRequest();
		 request.setAccountName(fromAddress);
		 request.setAcceptFormat(FormatType.JSON);
         request.setAddressType(0);
         request.setTagName("SysCode");
         request.setReplyToAddress(false);
         request.setToAddress(vo.getEmail());
         request.setSubject("高考志愿通");
         request.setHtmlBody(MessageFormat.format(htmlTemplate, vo.getEmail(), vo.getCode()));
		return request;
	}

	@Override
	public void sendSysCodeEmail(VerifyCodeVO vo) {
		String fromAddress = this.configManager.getProperty("no_reply_email", "no-reply@m.gaokaozyt.cn");
		String htmlTemplate = this.configManager.getProperty("syscode_template", "");
		String accessKey = this.configManager.getProperty("access_key", "");
		String access_key_secret = this.configManager.getProperty("access_key_secret", "");
		IClientProfile profile = DefaultProfile.getProfile("cn-beijing", accessKey, access_key_secret);
        IAcsClient client = new DefaultAcsClient(profile);
        
        SingleSendMailRequest req = convertToRequest(vo, fromAddress, htmlTemplate);
        try {
			SingleSendMailResponse httpResponse = client.getAcsResponse(req);
		} catch (ServerException e) {
			throw new AMSException("发送邮件错误", e);
		} catch (ClientException e) {
			throw new AMSException("发送邮件错误", e);
		}
	}

	@Override
	public void sendResetEmail(String email, String id) {
		String fromAddress = this.configManager.getProperty("no_reply_email", "no-reply@m.gaokaozyt.cn");
		String htmlTemplate = this.configManager.getProperty("reset_template", "");
		String accessKey = this.configManager.getProperty("access_key", "");
		String access_key_secret = this.configManager.getProperty("access_key_secret", "");
		IClientProfile profile = DefaultProfile.getProfile("cn-beijing", accessKey, access_key_secret);
        IAcsClient client = new DefaultAcsClient(profile);
        
        VerifyCodeVO vo = new VerifyCodeVO();
        vo.setEmail(email);
        vo.setCode("http://d.gaokaozyt.cn/user/resetpwd.do?id="+id);
		SingleSendMailRequest req = convertToRequest(vo , fromAddress, htmlTemplate);
        try {
			SingleSendMailResponse httpResponse = client.getAcsResponse(req);
		} catch (ServerException e) {
			throw new AMSException("发送邮件错误", e);
		} catch (ClientException e) {
			throw new AMSException("发送邮件错误", e);
		}
	}

}
